//package com.yunhe.device.service.device.impl;
//
//import com.yunhe.common.model.PageParam;
//import com.yunhe.common.util.StringUtil;
//import com.yunhe.device.domain.device.ConnectivityNode;
//import com.yunhe.common.exception.ArgumentErrorException;
//import com.yunhe.common.exception.ObjectNotFoundException;
//import com.yunhe.common.exception.UniqueConstraintsException;
//import com.yunhe.device.repository.authority.ResourceTreeRepository;
//import com.yunhe.device.repository.device.ConnectivityNodeRepository;
//import com.yunhe.device.service.device.ConnectivityNodeService;
//import com.yunhe.device.service.device.ModelService;
//import com.yunhe.common.util.QueryUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Sort;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.List;
//import java.util.Optional;
//
///**
// * 连接节点Service
// * @desc 连接节点服务类实现
// * @author liuronglei
// */
//@Service
//public class ConnectivityNodeServiceImpl implements ConnectivityNodeService {
//    @Autowired
//    private ConnectivityNodeRepository connectivityNodeRepository;
//    @Autowired
//    private ModelService modelService;
//    @Autowired
//    private ResourceTreeRepository resourceTreeRepository;
//
////    /**
////     * 设置端子列表
////     * @param connectivityNode 连接节点对象
////     */
////    private void setTerminalList(ConnectivityNode connectivityNode) throws ObjectNotFoundException {
////        List<Terminal> terminalList = connectivityNode.getTerminalList();
////        if (terminalList != null && terminalList.size() > 0) {
////            for (int i = 0; i < terminalList.size(); i++) {
////                Terminal terminal = terminalList.get(i);
////                if (terminal.getId() != null) {
////                    Terminal terminalTemp = terminalRepository.findById(terminal.getId());
////                    if (terminalTemp == null) {
////                        throw new ObjectNotFoundException("找不到ID为" + terminal.getId() + "的端子");
////                    }
////                    terminalList.set(i, terminalTemp);
////                } else if (terminal.getCode() != null) {
////                    Terminal terminalTemp = terminalRepository.findByCode(terminal.getCode());
////                    if (terminalTemp == null) {
////                        throw new ObjectNotFoundException("找不到代码为" + terminal.getCode() + "的端子");
////                    }
////                    terminalList.set(i, terminalTemp);
////                }
////            }
////        }
////    }
//
//    /**
//     * 连接节点对象处理和判断
//     * @param connectivityNode 连接节点对象
//     */
//    private void processBeforeSave(ConnectivityNode connectivityNode)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        //判断代码是否为空
//        if (connectivityNode.getCode() == null) {
//            throw new ArgumentErrorException("代码不能为空");
//        }
//        //判断代码是否已存在
//        ConnectivityNode connectivityNodeByCode = this.findByCode(connectivityNode.getCode());
//        if (connectivityNodeByCode != null) {
//            if (connectivityNode.getId() == null || connectivityNodeByCode.getId().longValue() != connectivityNode.getId().longValue()) {
//                throw new UniqueConstraintsException("该代码已存在");
//            }
//        }
//        //设置端子列表
////        setTerminalList(connectivityNode);
//    }
//
//    /**
//     * 新增连接节点
//     * @param connectivityNode 新增对象
//     */
//    @Override
//    @Transactional
//    public ConnectivityNode createConnectivityNode(ConnectivityNode connectivityNode)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        connectivityNode.setId(null);
//        processBeforeSave(connectivityNode);
//        ConnectivityNode newConnectivityNode = connectivityNodeRepository.save(connectivityNode);
////        resourceTreeRepository.bindResourceByFirmCode(firmCode, "ConnectivityNode", newConnectivityNode.getId());
////        svgCode.ifPresent(value -> modelService.addLabel(newConnectivityNode.getId(), value));
//        return newConnectivityNode;
//    }
//
//    /**
//     * 批量新增连接节点
//     * @param list 连接节点列表
//     */
//    @Override
//    @Transactional
//    public Iterable<ConnectivityNode> createConnectivityNode(List<ConnectivityNode> list)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        for (ConnectivityNode connectivityNode : list) {
//            connectivityNode.setId(null);
//            processBeforeSave(connectivityNode);
//        }
//        //新增连接节点
//        Iterable<ConnectivityNode> newConnectivityNodeList = connectivityNodeRepository.saveAll(list);
////        for (ConnectivityNode connectivityNode : newConnectivityNodeList) {
////            resourceTreeRepository.bindResourceByFirmCode(firmCode, "ConnectivityNode", connectivityNode.getId());
////            svgCode.ifPresent(value -> modelService.addLabel(connectivityNode.getId(), value));
////        }
//        return newConnectivityNodeList;
//    }
//
//    /**
//     * 根据id删除连接节点
//     * @param connectivityNodeId id
//     */
//    @Override
//    @Transactional
//    public void deleteConnectivityNode(Long connectivityNodeId) {
//        connectivityNodeRepository.deleteById(connectivityNodeId);
//    }
//
//    /**
//     * 根据代码删除连接节点
//     * @param code 代码
//     */
//    @Override
//    @Transactional
//    public Long deleteConnectivityNodeByCode(Long code) {
//        return connectivityNodeRepository.deleteByCode(code);
//    }
//
//    /**
//     * 更新连接节点
//     * @param connectivityNode 要更新的连接节点对象
//     */
//    @Override
//    @Transactional
//    public ConnectivityNode updateConnectivityNode(ConnectivityNode connectivityNode)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        boolean exists = connectivityNode.getId() != null && connectivityNodeRepository.existsById(connectivityNode.getId());
//        if (!exists) {
//            throw new ObjectNotFoundException("找不到ID为" + connectivityNode.getId() + "的连接节点");
//        }
//        processBeforeSave(connectivityNode);
//        return connectivityNodeRepository.save(connectivityNode);
//    }
//
//    /**
//     * 更新连接节点
//     * @param connectivityNodeId id
//     * @param code 代码
//     * @param name 编号
//     */
//    @Override
//    @Transactional
//    public ConnectivityNode updateConnectivityNode(Long connectivityNodeId,
//                                                   Long code,
//                                                   String name)
//            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
//        ConnectivityNode connectivityNode = this.findByConnectivityNodeId(connectivityNodeId);
//        if (connectivityNode == null) {
//            throw new ObjectNotFoundException("找不到ID为" + connectivityNodeId + "的连接节点");
//        }
//        Optional.ofNullable(code).ifPresent(connectivityNode::setCode);
//        Optional.ofNullable(name).ifPresent(connectivityNode::setName);
////        if (terminalIdList.isPresent()) {
////            //添加端子列表（此处只增加ID，在processBeforeSave中统一处理成对象）
////            List<Terminal> terminalList = new ArrayList<>();
////            for (Long terminalId : terminalIdList.get()) {
////                Terminal terminal = new Terminal();
////                terminal.setId(terminalId);
////                terminalList.add(terminal);
////            }
////            connectivityNode.setTerminalList(terminalList);
////        }
//        processBeforeSave(connectivityNode);
//        return connectivityNodeRepository.save(connectivityNode);
//    }
//
//    /**
//     * 根据ID获得连接节点对象
//     * @param id id
//     */
//    @Override
//    public ConnectivityNode findByConnectivityNodeId(Long id) {
//        return connectivityNodeRepository.findById(id).orElse(null);
//    }
//
//    /**
//     * 根据代码获得连接节点对象
//     * @param code 代码
//     */
//    @Override
//    public ConnectivityNode findByCode(Long code) {
//        return connectivityNodeRepository.findByCode(code);
//    }
//
//    /**
//     * 获得所有连接节点对象
//     * @param pageParam 分页对象
//     */
//    @Override
//    public Iterable<ConnectivityNode> findConnectivityNodes(PageParam pageParam) {
//        Iterable<ConnectivityNode> results;
//        if (QueryUtil.needPaging(pageParam)) {
//            results = connectivityNodeRepository.findAll(QueryUtil.getPageRequest(pageParam));
//        } else {
//            Sort sort = QueryUtil.getSort(pageParam.getSort());
//            if (sort != null) {
//                results = connectivityNodeRepository.findAll(sort);
//            } else {
//                results = connectivityNodeRepository.findAll();
//            }
//        }
//        return results;
//    }
//
//    /**
//     * 连接节点连接设备
//     * @param code 代码
//     * @param devCode 设备代码
//     * @param terminal 端子代码
//     */
//    @Override
//    public void bindDev(Long code, Long devCode, String terminal)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        if (!StringUtil.isEmpty(terminal)) {
//            connectivityNodeRepository.bindDev(code, devCode, terminal);
//        }else {
//            connectivityNodeRepository.bindDev(code, devCode, null);
//        }
//    }
//
//    /**
//     * 连接节点解绑设备
//     * @param code 代码
//     * @param devCode 设备代码
//     */
//    @Override
//    public void unbindDev(Long code, Long devCode)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        connectivityNodeRepository.unbindDev(code, devCode);
//
//    }
//
//    /**
//     * 根据代码获取连接节点绑定的设备
//     * @param code
//     * @return
//     * @throws ArgumentErrorException
//     * @throws UniqueConstraintsException
//     */
//    @Override
//    public Iterable search(Long code)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        return connectivityNodeRepository.findDevByCode(code);
//    }
//}
